package com.example.peibei.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.service.presenter.GetSmsPresenter;

import butterknife.BindView;
import butterknife.OnClick;

public class ChangePhoneActivity extends WDActivity {
    @BindView(R.id.et_phone)
    EditText et_phone;

    private GetSmsPresenter getSmsPresenter;
    private String phoneNum;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_phone;
    }

    @Override
    protected void initView() {
        getSmsPresenter = new GetSmsPresenter(new SmsCall());
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.bt_submit)
    public void submit() {
        phoneNum = et_phone.getText().toString();
        if (TextUtils.isEmpty(phoneNum)) {
            UIUtils.showToastSafe("请输入正确的手机号码");
            return;
        }
        if (UIUtils.isChinaPhoneLegal(phoneNum)) {
            getSmsPresenter.reqeust(phoneNum);
        } else {
            UIUtils.showToastSafe("请输入正确的手机号码");
        }
    }

    class SmsCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            UIUtils.showToastSafe("验证码已发送");
            Bundle bundle = new Bundle();
            bundle.putString("phone",phoneNum);
            intent(GetSmsActivity.class,bundle);
            finish();
        }

        @Override
        public void fail(ApiException e, Object... args) {
            UIUtils.showToastSafe(e.getDisplayMessage());
        }
    }

    @Override
    protected void destoryData() {

    }
}
